const NewsService = require('../../services/admin/NewsSevervice');

const NewsController = {
    //创建内容
    add: async (req, res) => {
        //图片
        const cover = req.file ? `/newsupload${req.file.filename}` : '';
        //解构内容
        const { title, content, category, isPublish, author } = req.body;
        await NewsService.add({
            title,
            content,
            category: Number(category),
            isPublish: Number(isPublish),
            cover,
            editTime: new Date(),
            author,
        });
        res.send({
            ActionType: 'OK',
        });
    },
    //获取内容列表
    getList: async (req, res) => {
        const result = await NewsService.getList({ _id: req.params.id });
        console.log(result);
        res.send({
            ActionType: 'OK',
            data: result,
        });
    },
    //获取内容
    postList: async (req, res) => {
        const author = req.params.aid;
        const result = await NewsService.postList({ author });
        res.send({
            ActionType: 'OK',
            data: result,
        });
    },
    //修改发布状态
    publish: async (req, res) => {
        await NewsService.publish({
            ...req.body,
            editTime: new Date(),
        });
        res.send({
            ActionType: 'OK',
        });
    },
    //删除文章
    delList: async (req, res) => {
        await NewsService.delList({ _id: req.params.id });
        res.send({
            ActionType: 'OK',
        });
    },
    //更新编辑的单个文章
    updateList: async (req, res) => {
        //图片
        const cover = req.file ? `/newsupload${req.file.filename}` : '';
        //解构内容
        const { title, content, category, isPublish, _id } = req.body;
        await NewsService.updateList({
            _id,
            title,
            content,
            category: Number(category),
            isPublish: Number(isPublish),
            cover,
            editTime: new Date(),
        });
        res.send({
            ActionType: 'OK',
        });
    },
};

module.exports = NewsController;
